import * as THREE from "../../../modules/three/three.module.js";
import dot_vertexGlsl from '../lib/shader/dot_vertex.glsl.js';
import slash_fragmentGlsl from '../lib/shader/slash_fragment.glsl.js';
import PluginBase from './PluginBase.js';

function getSlashMaterial() {
  const uniforms = {
    // 'lightColor': {value:  new THREE.Vector4(1, 1, 1, 1.0)},
    slashColor: { value: new THREE.Vector4(1.0, 0.1, 0.1, 1.0) },
    ratio: {
      value: new THREE.Vector2(
        100,
        (100 * window.innerHeight) / window.innerWidth,
      ),
    },
    density: { value: 1.0 },
  };
  const slashMaterial = new THREE.ShaderMaterial({
    uniforms: uniforms,
    vertexShader: dot_vertexGlsl,
    fragmentShader: slash_fragmentGlsl,
    side: THREE.DoubleSide,
    transparent: true,
  });
  return slashMaterial;
}

export default class SelectHoleAreaPlugin extends PluginBase {
  world3d;
  holeMesh;
  originMaterial;
  selectMaterial = getSlashMaterial();

  constructor(world3d) {
    super();
    this.world3d = world3d;
  }

  attachItem(holeMesh) {
    this.holeMesh = holeMesh;
    this.originMaterial = holeMesh.material;
    holeMesh.material = this.selectMaterial;
  }

  detachItem(data) {
    if (this.holeMesh) {
      this.holeMesh.material = this.originMaterial;
      this.holeMesh = null;
    }
  }

  pointerDown(event) {
    if (event.button === 2) {
      // mouse right button down
      return false;
    }
    this.detachItem()
    const store = this.world3d.store;
    const holeMesh = store.pickingHole();
    if (holeMesh && (store.stateFree || store.stateAddingHole)) {
      // 新增孔洞时，允许选中老孔洞
      this.attachItem(holeMesh);
    }
  }
}
